import { defineStore } from "pinia";
import request from "@/utils/request";
import { ApiRes, CategoryItem, SubCategory, TopCategory } from "@/types/data";
import { topCategory } from '../constants'

const defaultCategory = topCategory.map((item) => {
    return {
        name: item,
    }
})

const useHomeStore = defineStore('category', {
    state: () => ({
        categoryList: defaultCategory as CategoryItem[],
    }),
})



export default defineStore('category', {
    state: () => ({
        list: [] as CategoryItem[],
        topCategory: {} as TopCategory,
        subCategory: {} as SubCategory,
    }),

    actions: {
        // 获取所以有分类数据
        async getAllCategory() {
            const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
            this.list = res.data.result
        },
        // 获取一级分类
        async getTopCategory(id: string) {
            const res = await request.get<ApiRes<TopCategory>>('/category', {
                params: {
                    id,
                },
            })
            this.topCategory = res.data.result
        },
        async getSubFilter(id: string) {
            const res = await request.get<ApiRes<SubCategory>>(
                '/category/sub/filter',
                {
                    params: {
                        id,
                    },
                }
            )
            this.subCategory = res.data.result
        },
    },
    getters: {}
})